package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;


import android.content.Context;

import com.cmccgz.baselibrary.utils.NetUtils;
import com.cmccgz.baselibrary.utils.SharedPreferencesUtil;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.UserInfo;
import com.google.gson.Gson;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author INX
 * 用户信息presenter
 */
public class UserInfoPresenter implements UserInfoContract.UserInfoPresenter {
    private UserInfoContract.UserInfoView view;

    public UserInfoPresenter(UserInfoContract.UserInfoView view) {
        this.view = view;
    }

    @Override
    public void getUserInfo() {
        final Context context = GlobalContext.getInstance();
        try {
            String userInfoJson = SharedPreferencesUtil.getString(context, AppConfig.SP_USER_INFO_JSON+AppConfig.getMobile(context));
            UserInfo userInfo = new Gson().fromJson(userInfoJson, UserInfo.class);
            view.getUserInfoCache(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!NetUtils.isConnected(context)) {
            view.getUserInfoFail(context.getResources().getString(R.string.noNetWork));
            return;
        }
        String mobile = SharedPreferencesUtil.getString(context, AppConfig.SP_MOBILE);
        ApiService.getUserInfo(mobile)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.getUserInfoFail(context.getString(R.string.tips_userinfo_get_fail));
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)&&resultBean.resp!=null&&resultBean.resp instanceof UserInfo) {
                            UserInfo userInfo = (UserInfo) resultBean.resp;
                            view.getUserInfoSuccess(userInfo);
                            try {
                                SharedPreferencesUtil.putString(context,AppConfig.SP_USER_INFO_JSON+AppConfig.getMobile(context),new Gson().toJson(userInfo));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }else{
                            view.getUserInfoFail(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void start() {

    }
}
